<?php


class addfrdModel extends Model
{
	private $tb='sys_addfrd';

	//添加一条添加好友信息
	public function add($data)
	{
		$this->db->lock($this->tb);
		$add=$this->db->insert($this->tb,$data);
		$this->db->unlock();
		return $add;

	}

	//获取一条信息
	public function getone($uid)
	{
		$sql="select * from $this->tb where isread=0 and toid=$uid limit 1";
		return $this->db->getRow($sql);
	}
	public function getonebyid($vid)
	{
		$sql="select * from $this->tb where id=$vid";
		return $this->db->getRow($sql);
	}

	
	//查看我发出的且对方已经处理的验证消息(ownid=我的id,isadd不等于2,isdone=0的验证消息，查看后将消息设为完成)
	public function getmeone($uid)
	{
		$sql="select * from $this->tb where isadd<2 and isdone=0 and ownid=$uid limit 1";
		return $this->db->getRow($sql);
	}
	
	public function getall($uid)
	{
		$sql="select * from $this->tb where (ownid=$uid and isadd<2) or (toid=$uid) order by time desc";
		return $this->db->getAll($sql);
	}


	//获取所有未处理信息（未决定通过验证添加好友或未拒绝的验证信息）
	public function getdoing($uid)
	{
		$sql="select * from $this->tb where isdone=0 and toid=$uid order by time desc";
		return $this->db->getAll($sql);
	}
	
	//查找用户是否已经该好友未处理且未忽略的验证，有则返回该验证id，无则false
	public function hasverify($uid,$fid)
	{
		$sql="select id from $this->tb where isadd=2 and ownid=$uid and ignore_this=0";
		return $this->db->getOne($sql);
	}
	//更新一条验证信息
	public function updateone($vid,$data)
	{
		$this->db->lock($this->tb);
		if($this->db->update($this->tb,$data,$vid))
		{
			$this->db->unlock();
			return $this->db->affect_row();
		}
		else
		{
			return false;
		}
	}

	//将消息设为已读
	public function hasread($addid)
	{
		$this->db->lock($this->tb);
		$sql="update $this->tb set isread=1 where id=$addid limit 1";
		if($this->db->set_query($sql))
		{
			$this->db->unlock();
			return $this->db->affect_row();
		}
		else
		{
			return false;
		}
	}
	//忽略一条信息
	public function ignoreone($addid)
	{
		$this->db->lock($this->tb);
		$sql="update $this->tb set ignore_this=1 where id=$addid limit 1";
		if($this->db->set_query($sql))
		{
			$this->db->unlock();
			return $this->db->affect_row();
		}
		else
		{
			return false;
		}
	}

	//删除一条验证信息(未决定是否添加的即isadd=2才允许删除)
	public function delone($addid)
	{
		$sql="delete from  $this->tb where isadd=2 and id=$addid limit 1";
		if($this->db->set_query($sql))
		{
			return $this->db->affect_row();
		}
		else
		{
			return false;
		}
	}
	//删除一条验证信息
	public function delete($addid)
	{
		$sql="delete from  $this->tb where id=$addid limit 1";
		if($this->db->set_query($sql))
		{
			return $this->db->affect_row();
		}
		else
		{
			return false;
		}
	}

	//添加好友后，删除所有关于该好友的未处理验证信息(未决定是否添加的即isadd=2才允许删除)
	public function deletemore($uid,$fid)
	{
		$sql="delete from  $this->tb where isadd=2 and ownid=$fid and toid=$uid";
		if($this->db->set_query($sql))
		{
			return $this->db->affect_row();
		}
		else
		{
			return false;
		}
	}

	//同意一条信息
	public function accept($vid)
	{
		$t=$this->getonebyid($vid);
		if(!$t||$t['toid']==$t['ownid'])
		{
			return false;
		}
		$this->db->lock($this->tb);
		$sql="update $this->tb set isadd=1 where id=$vid limit 1";
		if($this->db->set_query($sql))
		{
			$this->db->unlock();	
			//同意后删除所有关于该好友的未处理验证信息(未决定是否添加的即isadd=2才允许删除)
			//$de=$this->deletemore($t['toid'],$t['ownid']);
			//if($de!==false)
			//{
				return $t;
			//}
			//return false;
		}
		return false;

	}
	//拒绝一条验证消息
	public function refuse($vid,$refusestr)
	{
		$this->db->lock($this->tb);
		$sql="update $this->tb set isadd=0,refuse_intro=\"$refusestr\" where id=$vid";

		if($this->db->set_query($sql))
		{
			$this->db->unlock();
			return true;
		}

		return false;
	}

	//将一条验证消息设为完成
	public function done($vid)
	{
		$this->db->lock($this->tb);
		$sql="update $this->tb set isdone=1 where id=$vid";
		if($this->db->set_query($sql))
		{
			$this->db->unlock();
			return true;
		}
		return false;
	}

	
	//将一条验证消息设为完全完成
	public function doneread($vid)
	{
		$this->db->lock($this->tb);
		$sql="update $this->tb set isdone=1,isread=1 where id=$vid";
		if($this->db->set_query($sql))
		{
			$this->db->unlock();
			return true;
		}
		return false;
	}



	//获取未读验证消息数
	public function getnum($uid)
	{
		$sql="select count(*) from $this->tb where isread=0 and toid=$uid";
		return $this->db->getOne($sql);
	}

	//获取验证的最后一条消息,不设已读
	public function getlast($uid)
	{
		$sql="select * from $this->tb where toid=$uid order by time desc limit 1";
		return $this->db->getRow($sql);
	}

}